/**
 * Created by IntelliJ IDEA.
 * User: jiyufei
 * Date: 2018/11/9
 * Time: 11:09
 **/
package com.jiyufei.elasticsearch;

import com.jiyufei.bean.SecureAnalysedLog;
import com.jiyufei.log.EsLogCenter;
import com.jiyufei.util.ElasticSearchInfo;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.JestResult;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.Index;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import io.searchbox.core.Update;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ElasticSearchOpertator {

    private JestClientFactory jestClientFactory;
    private JestClient jestClient;
    private EsLogCenter esLogCenter;
    
    private Search search;
    private SearchResult searchResult;
    private JestResult jestResult;
    private Index index;
    
    private  final static Logger logger = LoggerFactory.getLogger(ElasticSearchOpertator.class);
    
    
    public ElasticSearchOpertator(){
        jestClientFactory = new JestClientFactory();
        jestClientFactory.setHttpClientConfig(new HttpClientConfig.Builder(ElasticSearchInfo.ElasticSearchIp).multiThreaded(true).build());
        jestClient = jestClientFactory.getObject();
        esLogCenter = new EsLogCenter();
    }
    
    
    public int updateElasticSearch(String update,String index,String type,String id){
        try {
            jestResult = jestClient.execute(new Update.Builder(update).index(index).type(type).id(id).build());
            //System.out.println(jestResult.toString());
            if(jestResult.isSucceeded()){
                return 0;
            }
        }catch (Exception e){
            esLogCenter.addLog("LinuxLogAnalysis","","yunying",
                    "[ElasticSearchOpertator.updateElasticSearch]","High","更新ElasticSearch失败！");
            
        }
        logger.error("[ElasticSearchOpertator.updateElasticSearch]更新失败！index:" + index + ",type:" + type + ",id:" + id + ",Error Message:" + jestResult.getErrorMessage());
        return -1;
    }
    
    
    public SearchResult searchElasticSearch(String query , String index ,String type){
        
        if(type == null){
            search = new Search.Builder(query).addIndex(index).build();
        }else{
            search = new Search.Builder(query).addIndex(index).addType(type).build();
        }
        
        try {
            searchResult = jestClient.execute(search);
        }catch (Exception e){
            esLogCenter.addLog("LinuxLogAnalysis","","yunying",
                    "[ElasticSearchOpertator.searchElasticSearch]","High","查询ElasticSearch失败！");
        }
        return searchResult;
        
    }
    
    public void insertSecureAnalysedLog(SecureAnalysedLog secureAnalysedLog){
        try {
            index = new Index.Builder(secureAnalysedLog).index(ElasticSearchInfo.AnalysisIndex).type(secureAnalysedLog.getType()).build();
            jestClient.execute(index);
        }catch (Exception e){
            logger.error("[ElasticSearchOpertator.insertSecureAnalysedLog]添加数据错误。" + secureAnalysedLog.toString());
        }
    }
    

}
